//
//  LBChooseView.m
//  LandCredit
//
//  Created by 露露 on 2017/9/6.
//  Copyright © 2017年 联壁. All rights reserved.
//

#import "LBChooseView.h"



@interface LBChooseView ()
@property (nonatomic, strong,readwrite) LBChooseContentView *contenView;
@end

@implementation LBChooseView

//+(instancetype)chooseView{
//    LBChooseView *chooseView =
//    
//    return chooseView;
//}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initilzer];
    }
    return self;
}

- (void)initilzer
{
    self.frame = [[UIScreen mainScreen] bounds];
    self.backgroundColor = [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:.5];
    [self setWindowLevel:UIWindowLevelAlert];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initilzer];
    }
    return self;
}

-(void)show
{
    [self show:nil];
}
-(void)show:(void(^_Nullable)()) finished;
{
    [self makeKeyAndVisible];
    [self setHidden:NO];
    CGFloat height = 258 * K_view_scale;
    CGRect frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, height);
    self.contenView.frame = frame;
    self.contenView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.contenView];
    frame.origin.y -= height;
    [UIView animateWithDuration:0.25 animations:^{
        self.contenView.frame = frame;
    } completion:nil];
    
}
-(void)dismiss
{
    [self dismiss:nil];
}
-(void)dismiss:(void(^_Nullable)()) finished
{
    CGRect frame = self.contenView.frame;
    frame.origin.y += frame.size.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.contenView.frame = frame;
    } completion:^(BOOL finished) {
        [self setHidden:YES];
        [self resignKeyWindow];
        [self.contenView removeFromSuperview];
        self.contenView = nil;
        [self removeFromSuperview];
        
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}
#pragma mark Getter Method 
-(LBChooseContentView *)contenView
{
    if (!_contenView) {
        

        //改为代码加载ContentView
        _contenView = [[LBChooseContentView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 258 * K_view_scale)];
        
        
        // 获取bundle
//        NSBundle *resourceBundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[LBChooseContentView class]] pathForResource:@"LBChooseContentView" ofType:@"bundle"]];
//
//        _contenView = [[resourceBundle loadNibNamed:@"LBChooseContentView"
//                                       owner:nil
//                                     options:nil] firstObject];
        
        
        
//        没有使用私有化pods的时候,正常加载xib的方法
//        _contenView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LBChooseContentView class]) owner:nil options:nil] lastObject];
    }
    return _contenView;
}
-(void)setChooseArray:(NSArray<NSString *> *)chooseArray
{
    _chooseArray = chooseArray;
    self.contenView.chooseArray = chooseArray;
}
-(void)setTitle:(NSString *)title
{
    _title = title;
    self.contenView.titleLabel.text = _title;
}
@end
